/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.ui.base;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.AbstractBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import net.abbp.swing.util.ImageUtils;

/**
 * 颜色选择面板
 * 
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class ColorSelectionPane extends JPanel {

	private Color color;
	private boolean isCanBeNull;
	private JComponent displayComponent;
	private JButton triggleButton;
	private ColorControlWindow colorSetPopupWindow;
	private List changeListenerList;
	private final Color[] menuColors;
	private final Color[] menuColors2;

	public ColorSelectionPane(int paramInt) {
		this(paramInt, 24, 16);
	}

	public ColorSelectionPane(int paramInt1, int paramInt2, int paramInt3) {
		this.color = null;
		this.isCanBeNull = false;
		this.changeListenerList = new ArrayList();
		this.menuColors = new Color[] { Color.black, new Color(153, 51, 0), new Color(51, 51, 0), new Color(0, 51, 0),
				new Color(0, 51, 102), new Color(0, 0, 128), new Color(51, 51, 153), new Color(51, 51, 51),
				new Color(128, 0, 0), new Color(252, 102, 0), new Color(128, 128, 0), new Color(0, 128, 0),
				new Color(0, 128, 128), new Color(0, 0, 255), new Color(102, 102, 153), new Color(128, 128, 128),
				new Color(255, 0, 0), new Color(255, 153, 0), new Color(153, 204, 0), new Color(51, 153, 102),
				new Color(51, 204, 204), new Color(51, 102, 255), new Color(128, 0, 128), new Color(150, 150, 150),
				new Color(255, 0, 255), new Color(255, 204, 0), new Color(255, 255, 0), new Color(0, 255, 0),
				new Color(0, 255, 255), new Color(0, 204, 255), new Color(153, 51, 102), new Color(192, 192, 192),
				new Color(255, 153, 204), new Color(255, 204, 153), new Color(255, 255, 153), new Color(204, 255, 204),
				new Color(204, 255, 255), new Color(153, 204, 255), new Color(204, 153, 255), new Color(255, 255, 255) };
		this.menuColors2 = new Color[] { new Color(153, 153, 255), new Color(153, 51, 102), new Color(255, 255, 204),
				new Color(204, 255, 255), new Color(102, 0, 102), new Color(255, 128, 128), new Color(0, 102, 204),
				new Color(204, 204, 255), new Color(0, 0, 128), new Color(255, 0, 255), new Color(255, 255, 0),
				new Color(0, 255, 255), new Color(128, 0, 128), new Color(128, 0, 0), new Color(0, 128, 128),
				new Color(0, 0, 255) };
		setLayout(new BorderLayout());
		this.displayComponent = new JLabel();
		this.displayComponent.setBorder(BorderFactory.createLineBorder(Color.gray));
		this.triggleButton = new JButton(ImageUtils.readIcon("color/popupCenter.png"));
		this.triggleButton.setBorder(new TriggleLineBorder());
		JPanel localJPanel = new JPanel();
		localJPanel.setLayout(new BorderLayout(0, 0));
		localJPanel.add(this.displayComponent, BorderLayout.CENTER);
		localJPanel.setOpaque(true);
		this.displayComponent.setOpaque(true);
		this.displayComponent.addMouseListener(new PopupMouseListener());
		this.triggleButton.addMouseListener(new PopupMouseListener());
		add(localJPanel, BorderLayout.CENTER);
		add(this.triggleButton, BorderLayout.EAST);
		Dimension localDimension1 = new Dimension(paramInt3, paramInt2);
		this.triggleButton.setMaximumSize(localDimension1);
		this.triggleButton.setMinimumSize(localDimension1);
		this.triggleButton.setPreferredSize(localDimension1);
		this.triggleButton.setSize(localDimension1);
		Dimension localDimension2 = new Dimension(Math.max(paramInt1, localJPanel.getPreferredSize().width), paramInt2);
		localJPanel.setMaximumSize(localDimension2);
		localJPanel.setMinimumSize(localDimension2);
		localJPanel.setPreferredSize(localDimension2);
		localJPanel.setSize(localDimension2);
		addAncestorListener(new AncestorListener() {

			public void ancestorAdded(AncestorEvent paramAncestorEvent) {
			}

			public void ancestorRemoved(AncestorEvent paramAncestorEvent) {
				ColorSelectionPane.this.hidePopupMenu();
			}

			public void ancestorMoved(AncestorEvent paramAncestorEvent) {
				ColorSelectionPane.this.hidePopupMenu();
			}
		});
	}

	public Color getColor() {
		if (!(isEnabled())) {
			return null;
		}
		return this.color;
	}

	public void setColor(Color paramColor) {
		this.color = paramColor;
		if ((paramColor == null) || (paramColor.getAlpha() == 0)) {
			this.displayComponent.setBackground(null);
		} else {
			this.displayComponent.setBackground(paramColor);
		}
		if (this.changeListenerList.size() > 0) {
			ChangeEvent localChangeEvent = new ChangeEvent(this);
			for (int i = 0; i < this.changeListenerList.size(); ++i) {
				((ChangeListener) this.changeListenerList.get(i)).stateChanged(localChangeEvent);
			}
		}
		this.displayComponent.repaint();
	}

	public boolean isCanBeNull() {
		return this.isCanBeNull;
	}

	public void setCanBeNull(boolean paramBoolean) {
		this.isCanBeNull = paramBoolean;
	}

	public void setEnabled(boolean paramBoolean) {
		super.setEnabled(paramBoolean);
		this.displayComponent.setEnabled(paramBoolean);
		this.triggleButton.setEnabled(paramBoolean);
	}

	private void showPopupMenu() {
		if ((this.colorSetPopupWindow != null) && (this.colorSetPopupWindow.isVisible())) {
			hidePopupMenu();
			return;
		}
		if (!(isEnabled())) {
			return;
		}
		this.colorSetPopupWindow = getColorControlWindow();
		Point localPoint = new Point(0, 0);
		SwingUtilities.convertPointToScreen(localPoint, this);
		this.colorSetPopupWindow.setLocation(localPoint.x, localPoint.y + getSize().height);
		this.colorSetPopupWindow.setVisible(true);
	}

	private void hidePopupMenu() {
		if (this.colorSetPopupWindow != null) {
			this.colorSetPopupWindow.setVisible(false);
		}
		this.colorSetPopupWindow = null;
	}

	private ColorControlWindow getColorControlWindow() {
		if (this.colorSetPopupWindow == null) {
			Window localWindow = SwingUtilities.windowForComponent(this);
			if (localWindow != null) {
				this.colorSetPopupWindow = new ColorControlWindow(localWindow, isCanBeNull());
			}
		}
		return this.colorSetPopupWindow;
	}

	public void addColorChangeListener(ChangeListener paramChangeListener) {
		this.changeListenerList.add(paramChangeListener);
	}

	class TriggleLineBorder extends AbstractBorder {

		protected Insets borderInsets = new Insets(1, 0, 1, 0);

		public void paintBorder(Component paramComponent, Graphics paramGraphics, int paramInt1, int paramInt2,
				int paramInt3, int paramInt4) {
			paramGraphics.translate(paramInt1, paramInt2);
			paramGraphics.setColor(Color.gray);
			paramGraphics.drawLine(0, 0, paramInt3, 0);
			paramGraphics.drawLine(0, paramInt4 - 1, paramInt3, paramInt4 - 1);
			paramGraphics.drawLine(paramInt3 - 1, 0, paramInt3 - 1, paramInt4);
			paramGraphics.translate(-paramInt1, -paramInt2);
		}

		public Insets getBorderInsets(Component paramComponent) {
			return this.borderInsets;
		}
	}

	private class ColorCell extends JComponent {

		private Color color;

		public ColorCell() {
			this.color = Color.white;
			enableEvents(KeyEvent.MOUSE_EVENT_MASK);
			setPreferredSize(new Dimension(14, 14));
			setBorder(new LineBorder(Color.GRAY));
			setCursor(new Cursor(Cursor.HAND_CURSOR));
		}

		public ColorCell(Color paramColor) {
			this();
			setColor(paramColor);
		}

		public void setColor(Color paramColor) {
			this.color = paramColor;
			repaint();
		}

		public Color getColor() {
			return this.color;
		}

		@Override
		public void paintComponent(Graphics paramGraphics) {
			Dimension dim = getSize();
			Insets insets = getInsets();
			int i = dim.width - insets.left - insets.right;
			int j = dim.height - insets.top - insets.bottom;
			if (this.color == null) {
				paramGraphics.setColor(Color.white);
				paramGraphics.fillRect(insets.left, insets.top, dim.width - insets.left - insets.right, dim.height
						- insets.top - insets.bottom);
				paramGraphics.setColor(Color.gray);
				paramGraphics.drawLine(insets.left, insets.top, i, j);
				paramGraphics.drawLine(i, insets.top, insets.left, j);
			} else {
				paramGraphics.setColor(this.color);
				paramGraphics.fillRect(insets.left, insets.top, i, j);
			}
			if ((ColorSelectionPane.this.getColor() == null)
					|| (!(ColorSelectionPane.this.getColor().equals(getColor())))) {
				return;
			}
			paramGraphics.setColor(Color.BLACK);
			paramGraphics.drawRect(insets.left, insets.top, i - 1, j - 1);
		}

		public void processMouseEvent(MouseEvent paramMouseEvent) {
			if ((paramMouseEvent == null) || (paramMouseEvent.getID() == MouseEvent.MOUSE_RELEASED)) {
				ColorSelectionPane.this.setColor(getColor());
				hidePopupMenu();
			}
			if (paramMouseEvent == null) {
				return;
			}
			super.processMouseEvent(paramMouseEvent);
		}

		public Dimension getMinimumSize() {
			return getPreferredSize();
		}
	}

	class ColorSelectionPopupPane extends JPanel {

		public ColorSelectionPopupPane(boolean paramBoolean) {
			setLayout(new BorderLayout(0, 4));
			setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
			if (paramBoolean) {
				// localObject = new JButton(Inter.getLocText("Transparent"));
				// ((JButton) localObject).setCursor(new Cursor(12));
				// ((JButton) localObject).addActionListener(new
				// TransparentActionListener());
				// add((Component) localObject, "North");
			}
			Object localObject = new JPanel();
			add((Component) localObject, BorderLayout.CENTER);
			((JPanel) localObject).setLayout(new BoxLayout((Container) localObject, 1));
			JPanel localJPanel1 = new JPanel();
			((JPanel) localObject).add(localJPanel1);
			localJPanel1.setLayout(new GridLayout(5, 8, 5, 5));
			for (int i = 0; i < menuColors.length; ++i) {
				localJPanel1.add(new ColorCell(menuColors[i]));
			}
			((JPanel) localObject).add(Box.createVerticalStrut(5));
			((JPanel) localObject).add(new JSeparator());
			((JPanel) localObject).add(Box.createVerticalStrut(5));
			JPanel localJPanel2 = new JPanel();
			((JPanel) localObject).add(localJPanel2);
			localJPanel2.setLayout(new GridLayout(2, 8, 5, 5));
			for (int j = 0; j < ColorSelectionPane.this.menuColors2.length; ++j) {
				localJPanel2.add(new ColorCell(ColorSelectionPane.this.menuColors2[j]));
			}
			JButton localJButton = new JButton("自定义...");
			localJButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			localJButton.addActionListener(new CustomActionListener());
			add(localJButton, BorderLayout.SOUTH);
		}

		class CustomActionListener implements ActionListener {

			public void actionPerformed(ActionEvent paramActionEvent) {
				ColorSelectionPane.this.hidePopupMenu();
				Color localColor = JColorChooser.showDialog(SwingUtilities.getWindowAncestor(ColorSelectionPane.this),
						"Select Color", ColorSelectionPane.this.getColor());
				if (localColor == null) {
					return;
				}
				ColorSelectionPane.this.setColor(localColor);
			}
		}

		class TransparentActionListener implements ActionListener {

			public void actionPerformed(ActionEvent paramActionEvent) {
				Color localColor = new Color(255, 255, 255, 0);
				ColorSelectionPane.this.setColor(localColor);
				ColorSelectionPane.this.hidePopupMenu();
			}
		}
	}

	class ColorControlWindow extends JWindow {

		public ColorControlWindow(Window paramWindow, boolean paramBoolean) {
			super(paramWindow);
			initComponents(paramBoolean);
		}

		public void initComponents(boolean paramBoolean) {
			JPanel localJPanel = new JPanel();
			setContentPane(localJPanel);
			localJPanel.setBorder(UIManager.getBorder("PopupMenu.border"));
			localJPanel.setLayout(new BorderLayout());
			localJPanel.add(new ColorSelectionPopupPane(paramBoolean), BorderLayout.CENTER);
			pack();
		}
	}

	class PopupMouseListener extends MouseAdapter {

		public void mousePressed(MouseEvent paramMouseEvent) {
			ColorSelectionPane.this.showPopupMenu();
		}
	}
}